package co.fitstart.admin.dao;

import java.util.List;

import com.demo2do.core.persistence.annotation.Dynamic;
import com.demo2do.core.persistence.annotation.Execute;
import com.demo2do.core.persistence.annotation.Limit;
import com.demo2do.core.persistence.annotation.Query;
import com.demo2do.core.persistence.annotation.Variable;

import co.fitstart.entity.course.Course;
import co.fitstart.entity.courseware.fitnessgroup.BatchCoursewareTemplate;
import co.fitstart.entity.courseware.fitnessgroup.BatchCoursewareType;
import co.fitstart.entity.courseware.fitnessgroup.CoursewareMovementType;
import co.fitstart.entity.courseware.fitnessgroup.CoursewareTemplate;
import co.fitstart.entity.courseware.fitnessgroup.DailyCoursewareTemplate;
import co.fitstart.entity.courseware.fitnessgroup.FitnessGroup;
import co.fitstart.entity.courseware.fitnessgroup.FitnessGroupType;
import co.fitstart.entity.courseware.fitnessgroup.SpecialCoursewareTemplate;

@Dynamic("hibernate")
public interface GroupTemplateDao {
    
    /**
     * 
     * @param type
     * @param beginIndex
     * @param maxResult
     * @return
     */
    @Limit
    @Query("FROM FitnessGroup WHERE disabled = false AND type in (:type) ORDER BY createTime DESC")
    public List<FitnessGroup> listFitnessGroup(@Variable("type") FitnessGroupType[] type, @Variable("beginIndex") int beginIndex, @Variable("maxResult") int maxResult);
    
    /**
     * 
     * @param course
     * @return
     */
    @Query("FROM FitnessGroup WHERE disabled = false AND course = :course AND type != 2")
    public List<FitnessGroup> listFitnessGroup(@Variable("course") Course course);
    
    /**
     * 
     * @param type
     * @return
     */
    @Query("FROM FitnessGroup WHERE disabled = false AND type = :type")
    public List<FitnessGroup> listFitnessGroup(@Variable("type") FitnessGroupType type);
    
    /**
     * 
     * @param fitnessGroup
     * @return
     */
    @Query("FROM SpecialCoursewareTemplate WHERE disabled = false AND fitnessGroup = :fitnessGroup")
    public List<SpecialCoursewareTemplate> listSpecialCoursewareTemplate(@Variable("fitnessGroup") FitnessGroup fitnessGroup);
    
    /**
     * 
     * @param fitnessGroup
     * @return
     */
    @Query("FROM BatchCoursewareTemplate WHERE disabled = false AND fitnessGroup = :fitnessGroup")
    public List<BatchCoursewareTemplate> listBatchCoursewareTemplate(@Variable("fitnessGroup") FitnessGroup fitnessGroup);
    
    /**
     * 
     * @param type
     * @param fitnessGroup
     * @return
     */
    @Query("FROM BatchCoursewareTemplate WHERE disabled = false AND type = :type AND fitnessGroup = :fitnessGroup ORDER BY sorting ASC")
    public List<BatchCoursewareTemplate> listBatchCoursewareTemplate(@Variable("type") BatchCoursewareType type, @Variable("fitnessGroup") FitnessGroup fitnessGroup);
    
    /**
     * 
     * @param fitnessGroup
     * @param type
     * @param maxSorting
     * @return
     */
    @Query("FROM BatchCoursewareTemplate WHERE disabled = false AND fitnessGroup = :fitnessGroup AND type = :type AND sorting <= :maxSorting ORDER BY sorting ASC")
    public List<BatchCoursewareTemplate> listBatchCoursewareTemplate(@Variable("fitnessGroup") FitnessGroup fitnessGroup, @Variable("type") BatchCoursewareType type, @Variable("maxSorting") int maxSorting);
    
    /**
     * 
     * @param type
     * @param sorting
     * @return
     */
    @Query("FROM BatchCoursewareTemplate WHERE disabled = false AND fitnessGroup = :fitnessGroup AND type = :type AND sorting = :sorting")
    public BatchCoursewareTemplate loadBatchCoursewareTemplate(@Variable("fitnessGroup") FitnessGroup fitnessGroup, @Variable("type") BatchCoursewareType type, @Variable("sorting") int sorting);
    
    /**
     * 
     * @param batches
     * @return
     */
    @Query("FROM DailyCoursewareTemplate WHERE disabled = false AND batch in (:batches)")
    public List<DailyCoursewareTemplate> listDailyCoursewareTemplate(@Variable("batches") List<BatchCoursewareTemplate> batches);
    
    /**
     * 
     * @param batch
     * @return
     */
    @Query("FROM DailyCoursewareTemplate WHERE disabled = false AND batch = :batch ORDER BY sorting")
    public List<DailyCoursewareTemplate> listDailyCoursewareTemplate(@Variable("batch") BatchCoursewareTemplate batch);
    
    /**
     * 
     * @param batch
     * @param sorting
     * @return
     */
    @Query("FROM DailyCoursewareTemplate WHERE disabled = false AND batch = :batch AND sorting = :sorting")
    public DailyCoursewareTemplate loadDailyCoursewareTemplate(@Variable("batch") BatchCoursewareTemplate batch, @Variable("sorting") int sorting);
    
    /**
     * 
     * @param dailyCoursewareTemplate
     */
    @Execute("UPDATE CoursewareTemplate SET disabled = true WHERE daily = :daily")
    public void deleteCoursewareTemplate(@Variable("daily") DailyCoursewareTemplate dailyCoursewareTemplate);
    
    /**
     * 
     * @param dailyCoursewareTemplates
     */
    @Execute("UPDATE CoursewareTemplate SET disabled = true WHERE daily in (:dailies)")
    public void deleteCourseware(@Variable("dailies") List<DailyCoursewareTemplate> dailyCoursewareTemplates);
    
    /**
     * 
     * @param specialCoursewareTemplates
     */
    @Execute("UPDATE CoursewareTemplate SET disabled = true WHERE special in (:specials)")
    public void deleteSpecialCourseware(@Variable("specials") List<SpecialCoursewareTemplate> specialCoursewareTemplates);
    
    /**
     * 
     * @param specialCoursewareTemplate
     */
    @Execute("UPDATE CoursewareTemplate SET disabled = true WHERE special = :special")
    public void deleteCoursewareTemplate(@Variable("special") SpecialCoursewareTemplate specialCoursewareTemplate);
    
    /**
     * 
     * @param fitnessGroup
     */
    @Execute("UPDATE BatchCoursewareTemplate SET disabled = true WHERE fitnessGroup = :fitnessGroup")
    public void deleteBatchCoursewareTemplate(@Variable("fitnessGroup") FitnessGroup fitnessGroup);
    
    /**
     * 
     * @param dailyCoursewareTemplate
     * @return
     */
    @Query("FROM CoursewareTemplate WHERE disabled = false AND daily = :daily ORDER BY ranking ASC,id ASC")
    public List<CoursewareTemplate> listCoursewareTemplate(@Variable("daily") DailyCoursewareTemplate dailyCoursewareTemplate);
    
    /**
     * 
     * @param specialCoursewareTemplate
     * @return
     */
    @Query("FROM CoursewareTemplate WHERE disabled = false AND special = :special ORDER BY id")
    public List<CoursewareTemplate> listCoursewareTemplate(@Variable("special") SpecialCoursewareTemplate specialCoursewareTemplate);
    
    /**
     * 
     * @param fitnessGroup
     * @return
     */
    @Query("FROM SpecialCoursewareTemplate WHERE disabled = false AND fitnessGroup = :fitnessGroup")
    public SpecialCoursewareTemplate loadSpecialCoursewareTemplate(@Variable("fitnessGroup") FitnessGroup fitnessGroup);

    /**
     * 
     * @param dailyCoursewareTemplate
     * @param type
     * @return
     */
    @Query("FROM CoursewareTemplate WHERE disabled=false AND daily=:daily AND type=:type ORDER BY id ASC")
    public List<CoursewareTemplate> listCoursewareTemplate(@Variable("daily") DailyCoursewareTemplate dailyCoursewareTemplate, @Variable("type") CoursewareMovementType type);
    
    /**
     * 
     * @param dailyCoursewareTemplate
     * @param type
     * @param ranking
     */
    @Execute("UPDATE CoursewareTemplate SET ranking=ranking-1 WHERE disabled=false AND daily=:daily AND type=:type AND ranking>:ranking")
    public void riseRankingBehind(@Variable("daily") DailyCoursewareTemplate dailyCoursewareTemplate, @Variable("type") CoursewareMovementType type, @Variable("ranking") int ranking);
    
    /**
     * 
     * @param dailyCoursewareTemplate
     * @param type
     * @param ranking
     * @return
     */
    @Query("FROM CoursewareTemplate WHERE disabled=false AND daily=:daily AND type=:type AND ranking=:ranking")
    public CoursewareTemplate getCoursewareTemplate(@Variable("daily") DailyCoursewareTemplate dailyCoursewareTemplate, @Variable("type") CoursewareMovementType type, @Variable("ranking") int ranking);
    
    
}
